<?php
/**
 * Common
 * 
 * @category	Util
 * @package		dayscript.util.dynamic
 * @subpackage	
 * @author		Nelson Daza <ndaza@dayscript.com>
 * @copyright	2009 Dayscript Ltda.
 * @license		
 * @version		1.0
 * @version		$Revision: 0 $
 * @filesource	
 * @link		http://www.dayscript.com
 * @link		{docsLink}
 * @uses		
 * @since		1.0
 * @modifiedby	$LastChangedBy: Nelson Daza $
 * @modified	$Date: 2009-08-13 $
 */

namespace dayscript\util\dynamic;

/**
 * Common
 * 
 * Abstract
 * 
 * @category	Util
 * @package		dayscript.util.dynamic
 * @subpackage	
 * @author		Nelson Daza <ndaza@dayscript.com>
 * @copyright	2009 Dayscript Ltda.
 * @license		
 * @version		1.0
 * @link		http://www.dayscript.com
 * @link		{docsLink}
 * @uses		\dayscript\exception\ErrorCallerException
 * @since		1.0
 * @modifiedby	$LastChangedBy: Nelson Daza $
 * @modified	$Date: 2009-08-13 $
 */
abstract class Common	{
	/**
	 * Calls a protected method.
	 *
	 * Not to be used by the user.
	 *
	 * @param string $method
	 * @return mixed
	 */
	function __callMethod( $method )	{
		if( \method_exists( $this, $method ) )	{
			/*
			if( \count( debug_backtrace( )) < 5)
				return null;
			*/
			$arguments = \func_get_args( );
			\array_shift( $arguments );
			return \call_user_func_array( array( $this, $method ), $arguments );
		}
		else if( \property_exists( $this, $method ) )	{
			throw new \dayscript\exception\ErrorCallerException( 'Call to a property as a method "' . $method . '"', \E_USER_ERROR );
		}
		throw new \dayscript\exception\ErrorCallerException( 'Call to undefined method "' . $method . '"', \E_USER_ERROR );
	}
	/**
	 * Gets the value of a protected property.
	 * 
	 * Not to be used by the user.
	 *
	 * @param string $property
	 * @return mixed
	 */
	function __getProperty( $property )	{
		if( \property_exists( $this, $property ) )	{
			/*
			if( \count( debug_backtrace()) < 4)
				return null;
			*/
			return $this->$property;
		}
		else if( \method_exists( $this, $property ) )	{
			throw new \dayscript\exception\ErrorCallerException( 'Call to a method as a property "' . $property . '"', \E_USER_ERROR );
		}
		throw new \dayscript\exception\ErrorCallerException( 'Call to undefined property "' . $property . '"', \E_USER_ERROR );
	}
	/**
	 * Sets the value of a protected property. Not to be used by the user.
	 *
	 * @param string $property
	 * @param mixed $value
	 */
	function __setProperty( $property, $value )	{
		if( \property_exists( $this, $property ) )	{
			/*
			if(count(debug_backtrace()) < 3)
				return null;
			*/
			$this->$property = $value;
		}
		else if( \method_exists( $this, $property ) )	{
			throw new \dayscript\exception\ErrorCallerException( 'Call to a method as a property "' . $property . '"', \E_USER_ERROR );
		}
		throw new \dayscript\exception\ErrorCallerException( 'Call to undefined property "' . $property . '"', \E_USER_ERROR );
	}
}
